Promise.all2 = (promises) => {
    return new Promise((resolve, reject)=>{
        let len = promises.length
        const results = new Array(len)
        let count = 1
        // index 用于保证结果顺序
        promises.forEach((promise, index) => {
        // 对p进行一次包装，防止非Promise对象
            Promise.resolve(promise).then(res => {
                    results[index] = res
                    if(count === len) resolve(results)
                    count += 1
                }).catch(err => {
                    reject(err)
                })
        });
      
    })
}

// 测试
const p1 = new Promise((resolve, reject) => {
    setTimeout(()=>{
        resolve(1)
    }, 1000)
})
const p2 = Promise.resolve(2)
const p3 = 3
const p4 = new Date()
const promises = [p1, p2, p3, p4]

const pp = Promise.all(promises)
pp.then(res=>{
    console.log(res)
})

const pp2 = Promise.all2(promises)
pp2.then(res=>{
    console.log(res)
}).catch(err => {
    console.log(err)
})


// console.log(typeof p2 === 'object')
// console.log(p2 instanceof Promise )
